#ifndef SONGMANAGER_H
#define SONGMANAGER_H

#include <QObject>
#include <QMap>
#include <QSet>
#include <QUuid>
#include <QSqlDatabase>
#include "songitem.h"
#include "songitemwidget.h"

class SongManager : public QObject
{
    Q_OBJECT
public:
    static SongManager* instance() {
        if (_songManager == nullptr)
            _songManager = new SongManager;
        return _songManager;
    }
signals:
    void played(const QUrl& url, bool isPlay);
    void liked(const QUrl& url, bool isLike);
    void downloaded(const QUrl& url, const QString& path);
private:
    explicit SongManager(QObject *parent = nullptr);
    SongManager(const SongManager&) = delete;
    static SongManager* _songManager;

    void initDir();
    void initDatabase();
    void loadSongs();
public:
    void addSongItem(SongItem& songItem);
    bool getSongItem(SongItem* songItem);
    bool getSongItem(SongItem* songItem, const QUrl& url);
    void download(qint64 id);
    const SongItem& getSongItem(const QUrl& url);
    bool exist(const QUrl& url);
    void addToList(const QUrl& url, int listIndex);
    void play(const QUrl& url, bool isPlay);
    void like(const QUrl& url, bool isLike);
    int countOfObject() { return _hashSongs.size(); }

    void updateSongList(int listId, const QList<QUrl>& songList);
    const QList<QUrl>& getSongList(int listId);
    void addSongToList(int listId, const QUrl& url);
    void deleteSongFromList(int listId, const QUrl& url);

    void storage();
private:
    QSqlDatabase _sqlite;
//    QMap<QUuid, SongItem> _hashSongs;
//    QMap<QUrl, QUuid> _hashUrl;
    QMap<QUrl, SongItem*> _hashSongs;
    QMap<int, QList<QUrl>> _hashLists;
//    QVector<playList>
};

#endif // SONGMANAGER_H
